<template>
	<view class="youhuiquan-box">
		<view :class="['youhuiquan-list ',{'active':currid==item.id}]" v-for="(item, index) in youhuiquan"
			:key="item.id">
			<view class="youhuiquan-box-left">
				<view class="youhuiquan-price" v-if="item.yhType==0">
					<text style="font-size: 0.3rem;">￥</text>{{item.yhJine}}
				</view>
				<view class="youhuiquan-price" v-else>
					{{item.yhZhekou}}<text style="font-size: 0.3rem;">折</text>
				</view>
				<view class="youhuiquan-qixian">
					{{item.des}}
				</view>
			</view>
			<view class="youhuiquan-box-body">
				<view class="youhuiquan-name">{{item.title}}</view>
				<view class="youhuiquan-youxiaoqi">有效期 ：{{item.stime}}-{{item.etime}}</view>
			</view>
			<view class="youhuiquan-box-right">
				<view :value="item.id.toString()" @click="change(item)"
					:class="['m-list-right-radio', { 'm-list-right-radio-active': currid==item.id }]">
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currid: 0
			}
		},
		methods: {
			change(item) {
				var jibie = this.ordertype;
				if (jibie == 103)
					jibie = 18;
				else if (jibie == 104)
					jibie = 19;

				var month = this.count;
				if (this.danweitype == 1)
					month = 12 * this.count;
				//金额优惠
				if (item.yhType == 0 && item.yhJine >= this.Discount) {
					uni.showToast({
						title: '付款金额须大于优惠券金额才能使用！',
						icon: "none"
					});
					return;
				} else if (item.limitMinJine > 0 && item.limitMinJine > this.Discount) {
					uni.showToast({
						title: '付款金额满' + item.limitMinJine + '元才能使用该优惠券！',
						icon: "none"
					});
					return;
				} else if (item.limitMaxJine > 0 && item.limitMaxJine < this.Discount) {
					uni.showToast({
						title: '付款金额高于' + item.limitMaxJine + '元不能使用该优惠券！',
						icon: "none"
					});
					return;
				} else if (item.limitMonth > 0 && item.limitMonth > month) {
					let str = '';
					if (item.limitMonth >= 12) {

						str = '服务周期需满' + parseInt(item.limitMonth / 12) + '年才能使用该优惠券！';
					} else {
						str = '服务周期需满' + item.limitMonth + '个月才能使用该优惠券！';
					}
					uni.showToast({
						title: str,
						icon: "none"
					});
					return;
				} else if (item.limitRoMo != null && item.limitRoMo.length > 0 && item.limitRoMoType == 0 && !this
					.isInArray(item.limitRoMo,
						jibie)) {
					uni.showToast({
						title: '该优惠券仅限定的会员级别可用，请查看使用条件！',
						icon: "none"
					});
					return;
				} else if (item.limitQuanguo.toString().toLowerCase() == "true" && this.isquanguo == 0) {
					uni.showToast({
						title: '该优惠券需要购买全国会员才能使用！',
						icon: "none"
					});
					return;
				}
				if (this.currid == item.id) {
					this.currid = 0;
				} else {
					this.currid = item.id;
				}
				this.$emit('Change', this.currid);
			},
			///Discount:金额  isquanguo：是否全国  1：是 0 否
			check(Discount, isquanguo, ordertype, count, danweitype) {
				let curritem = this.youhuiquan.filter(item => {
					return item.id == this.currid;
				})
				//console.log(curritem);


				var jibie = ordertype;
				if (jibie == 103)
					jibie = 18;
				else if (jibie == 104)
					jibie = 19;

				var month = count;
				if (danweitype == 1)
					month = 12 * count;
				if (curritem.length > 0) {
					if (curritem[0].yhType == 0 && curritem[0].yhJine >= Discount) {
						this.currid = 0;
					} else if (curritem[0].limitMinJine > 0 && curritem[0].limitMinJine > Discount) {
						this.currid = 0;
					} else if (curritem[0].limitMaxJine > 0 && curritem[0].limitMaxJine < Discount) {
						this.currid = 0;
					} else if (curritem[0].limitMonth > 0 && curritem[0].limitMonth > month) {
						this.currid = 0;
					} else if (curritem[0].limitRoMo != null && curritem[0].limitRoMo.length > 0 && curritem[0]
						.limitRoMoType == 0 && !this.isInArray(
							curritem[0].limitRoMo, jibie)) {
						this.currid = 0;
					} else if (curritem[0].limitQuanguo.toString().toLowerCase() == "true" && isquanguo == 0) {
						this.currid = 0;
					}
				}
				this.$emit('Change', this.currid);
			},
			isInArray(arr, value) {
				for (var i = 0; i < arr.length; i++) {
					if (value === arr[i]) {
						return true;
					}
				}
				return false;
			}
		},
		props: {
			youhuiquan: {
				type: Array,
				defual: function() {
					return [];
				}
			},
			//价格
			Discount: {
				type: String,
				defual: "0"
			},
			//会员级别
			ordertype: {
				type: Number,
				defual: 0
			},
			//是否全国会员 1：是 0 否
			isquanguo: {
				type: Number,
				defual: 0
			},
			//选中的月份
			count: {
				type: Number,
				defual: 1
			},
			//单位 1：年 2：月
			danweitype: {
				type: Number,
				defual: 0
			},
		}
	}
</script>

<style lang="scss" scoped>
	.m-list {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;

		&-right {
			margin-right: 24rpx;

			&-radio {
				box-sizing: border-box;
				width: 36rpx;
				height: 36rpx;
				border-radius: 50%;
				border: 2rpx solid #A6A6A6;

				&-active {
					background-image: url(/static/img/huiyuan/yhqchecked.png);
					background-size: 100%;
					border: none;
				}
			}
		}
	}

	.youhuiquan-box {
		margin: 30rpx 30rpx 0;
		padding-bottom: 30rpx;
	}

	.youhuiquan-box .youhuiquan-list {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
		color: #898989;
		justify-content: space-between;
		background: url(/static/img/youhuiquan_bg1.png) no-repeat;
		height: 138rpx;
		background-size: contain;
	}

	.youhuiquan-box .youhuiquan-list .youhuiquan-box-left {
		width: 160rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.youhuiquan-price {
		font-size: 45rpx;
		color: #f6b8b8;
	}

	.youhuiquan-qixian {
		font-size: 24rpx;
	}

	.youhuiquan-box .youhuiquan-list .youhuiquan-box-body {
		margin-left: 35rpx;
		flex: 1;
	}

	.youhuiquan-box-body .youhuiquan-name {
		color: #282828;
		font-size: 30rpx;
		margin-bottom: 8rpx;
	}

	.youhuiquan-box-body .youhuiquan-youxiaoqi {
		color: #5f5f5f;
		font-size: 20rpx;
	}

	.youhuiquan-box .youhuiquan-list .youhuiquan-box-right {
		margin-right: 30rpx;
	}

	.fwzq-view-cell .youhuiquan-list .zhitongcheL {
		margin-left: 30rpx;
		margin-top: 15rpx;
	}

	.youhuiquan-box .active {
		background: url(/static/img/youhuiquan_bg2.png) no-repeat;
		height: 138rpx;
		background-size: contain;
	}

	.youhuiquan-box .active .youhuiquan-box-left {
		color: #ff1440;
	}
	.youhuiquan-box .active .youhuiquan-box-left .youhuiquan-price {
		color: #ff1440;
	}
</style>
